<template>

  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      ref="queryFormRef"
      :inline="true"
      :model="queryParams"
      class="-mb-15px"
      label-width="68px"
    >
	  <el-form-item label="归属年份" prop="inyear">
	    <el-select v-model="queryParams.inyear" placeholder="请选择归属年份" clearable class="!w-240px">
	      <el-option v-for="year in years" :key="year" :label="year" :value="year" />
	    </el-select>
	  </el-form-item>
	  <el-form-item label="会员职级" prop="tagIds">
	    <MemberTagSelect v-model="queryParams.tagIds" />
	  </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery">
          <Icon class="mr-5px" icon="ep:search" />
          搜索
        </el-button>
        <el-button @click="resetQuery">
          <Icon class="mr-5px" icon="ep:refresh" />
          重置
        </el-button>
		<el-button type="primary" @click="openForm('create')">
		  <Icon icon="ep:plus" class="mr-5px" /> 新增
		</el-button>
		<el-button type="primary" @click="initData">
		  <Icon class="mr-5px" icon="ep:edit" />
		  拷贝上一年的配置数据
		</el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table
      v-loading="loading"
      :data="list"
      :show-overflow-tooltip="true"
      :stripe="true"
      @selection-change="handleSelectionChange"
    >
	  <el-table-column align="center" label="会员职级" prop="tagName" />
	  <el-table-column align="center" label="归属年份" prop="inyear" />
	  <el-table-column
	    :formatter="amountFormatter"
	    align="center"
	    label="会费金额(元)"
	    prop="amount"
	  />
	  <el-table-column align="center" label="备注" prop="remark" />
      <el-table-column
        :show-overflow-tooltip="false"
        align="center"
        fixed="right"
        label="操作"
      >
        <template #default="scope">
          <div class="flex items-center justify-center">
			<template v-if="scope.row.id">
				<el-button link type="primary" @click="handleCommand('handleUpdate',scope.row)">编辑</el-button>
				<el-button link type="primary" @click="handleDelete(scope.row.id)">删除</el-button>
			</template>
			<template v-else>
				<el-button link type="primary" @click="handleCommand('handleCreate',scope.row)">去设置</el-button>
			</template>
          </div>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      v-model:limit="queryParams.pageSize"
      v-model:page="queryParams.pageNo"
      :total="total"
      @pagination="getList"
    />
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <UpdateForm ref="formRef" @success="getList" />
</template>
<script lang="ts" setup>
import * as ChamberApi from '@/api/chamber'
import { DICT_TYPE } from '@/utils/dict'
import UpdateForm from './UpdateForm.vue'
import MemberTagSelect from '@/views/member/tag/components/MemberTagSelect.vue'
import { checkPermi } from '@/utils/permission'

defineOptions({ name: 'MemberFeeSet' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const total = ref(0) // 列表的总页数
const list = ref([]) // 列表的数据
const queryParams = reactive({
  pageNo: 1,
  pageSize: 20,
  inyear: new Date().getFullYear(),
  tagIds: [],
})
const queryFormRef = ref() // 搜索的表单
const selectedIds = ref<number[]>([]) // 表格的选中 ID 数组
const years = computed(()=> {
	let x = [];
	for(let i=(new Date().getFullYear())+1; i>=2015 ; i--) {
		x.push(i);
	}
	return x;
});
/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await ChamberApi.getMemberFeeConfigList(queryParams)
    list.value = data
  } finally {
    loading.value = false
  }
}
function amountFormatter(_row: any, _column: any, cellValue: any): string {
  return _row.id ? cellValue : '未设置'
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}
/** 拷贝上一年的配置数据 */
const initData = async() => {
  const data = await ChamberApi.initMemberFeeConfig();
  message.success('同步成功')
}

/** 打开会员详情 */
const { push } = useRouter()
const openDetail = (id: number) => {
  push({ name: 'MemberUserDetail', params: { id } })
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id)
}

/** 表格选中事件 */
const handleSelectionChange = (rows: ChamberApi.MemberFeeConfigVo[]) => {
  selectedIds.value = rows.map((row) => row.id)
}

/** 操作分发 */
const handleCommand = (command: string, row: ChamberApi.MemberFeeConfigVo) => {
  switch (command) {
    case 'handleUpdate':
      openForm('update', row.id)
      break
    case 'handleCreate':
      openForm('create', row.tagId)
      break
    default:
      break
  }
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await ChamberApi.deleteMemberFeeConfig(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

/** 初始化 **/
onMounted(() => {
  getList()
})
</script>
